package com.tanhua.app.controller;

import com.tanhua.app.service.UserService;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.pojo.UserInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //01 登录--获取验证码
    //post /user/login
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) throws Exception {
        //1.获取请求参数 手机号
        String phone = (String) map.get("phone");
        //2.调用Service执行操作
        userService.login(phone);
        //返回结果
        return ResponseEntity.ok(null);
    }

    //02登录 --验证码校验
    //Post /user/loginverification
    @PostMapping("loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map param){
        //1.解析参数
        String  mobile = (String) param.get("phone");
        String  verificationCode = (String) param.get("verificationCode");
        //2.调用service执行操作
       Map retMap = userService.loginVerification(mobile,verificationCode);
        //3.返回结果
        return ResponseEntity.ok(retMap);
    }

    //03首次登录 ---完善资料
    //post /user/loginReginfo
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                                       @RequestHeader("Authorization")String token){
        //1.校验用户是否登录
        boolean verifyToken = JwtUtils.verifyToken(token);
        //如果用户未登录,校验失败,直接返回401
        if (!verifyToken){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
            //return ResponseEntity.status(401).body(null);
        }
        //2.从token中获取当前用户id 设置到userinfo中
        Claims claims = JwtUtils.getClaims(token);
        Long id =Long.parseLong(claims.get("id").toString());
        userInfo.setId(id);
        //3.调用service执行操作
        userService.loginReginfo(userInfo);
        //4.返回数据
        return ResponseEntity.ok(null);
    }

    //4.首次登录 --补充头像
    //post/user/loginReginfo/head
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto,
                               @RequestHeader("Authorization") String token) throws IOException {
        //1.校验用户是否登录
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
        //2.解析token 获取用户主键id
        Claims claims = JwtUtils.getClaims(token);
        Long id = Long.parseLong(claims.get("id").toString());
        //3.调用service实现功能
        userService.upload(headPhoto,id);
        //4.返回结果
        return ResponseEntity.ok(null);
    }

}
